<?php
//============================================================+
// File name   : qrcode.class.php
// Begin       : 2011-01-01
// Last Update : 2011-02-26
// Version     : 0.1.2
// License     : GNU LGPL (http://www.gnu.org/copyleft/lesser.html)
// 	----------------------------------------------------------------------------
//  Copyright (C) 2011 by Stefan Mayr, code@grizzly.cc
// 	
// 	This program is free software: you can redistribute it and/or modify
// 	it under the terms of the GNU Lesser General Public License as published by
// 	the Free Software Foundation, either version 2.1 of the License, or
// 	(at your option) any later version.
// 	
// 	This program is distributed in the hope that it will be useful,
// 	but WITHOUT ANY WARRANTY; without even the implied warranty of
// 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// 	GNU Lesser General Public License for more details.
// 	
// 	You should have received a copy of the GNU Lesser General Public License
// 	along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 	
// 	See LICENSE.TXT file for more information.
//  ----------------------------------------------------------------------------
//
// Description : PHP class for generating QR barcodes specially for social media 
//               plattforms like Facebook, Twitter and YouTube
//
// Author: Stefan Mayr
//
//============================================================+

class SocialQrCode {
	
	var $download_path;
	
	const QR_API_URL = "http://api.2fb.co/";
	const QR_SIZE = 20;
	
	public function get_qr_code($social_profile_url, $dest_directory = false, $dest_file = false) {
		return $this->download(self::QR_API_URL."?type=PNG&size=".self::QR_SIZE."&url=".$social_profile_url, $dest_directory, $dest_file);
	} 
	
	private function download($url, $dest_directory = false, $dest_file = false) {
		
		$stream = null;
		
		if ($dest_directory) {
			$this->make_dir ( $dest_directory, 777 );
			if (is_dir($dest_directory)) {
				$stream = fopen ( $dest_directory.$dest_file, 'wb' );
			}
		} else {
			$stream = false;
		}
		
		$response = $this->download_curl ( $url, $stream );
		if (! $response) {
			$response = $this->download_fopen ( $url, $stream );
			if (! $response) {
				$response = $this->download_fsock ( $url, $stream );
			}
		}
		if ($stream) {
			return null;
		} else {
			$this->show_qr_code($response);
		}
		
	}
	
	private function show_qr_code($data) {
		header('Content-Type: image/png');
		echo $data;
	}
	
	private function download_curl($url, $stream = false) {
		if (! function_exists ( 'curl_init' ))
			return false;
			
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		curl_setopt ( $ch, CURLOPT_FAILONERROR, 1 );
		
		if ($stream) {
			curl_setopt ( $ch, CURLOPT_FILE, $stream );
		} else{
			curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		}
		
		$r = curl_exec ( $ch );
		curl_close ( $ch );
		return $r;
	}
	
	private function download_fopen($url, $stream = false) {
		if (! $file = @fopen ( $url, 'rb' ))
			return false;
		
		if ($stream) {
			$r = true;
			while ( $block = fread ( $file, 512 ) )
				fwrite ( $stream, $block );
		} else {
			$r = '';
			while ( $block = fread ( $file, 512 ) )
				$r .= $block;
		}
		
		fclose ( $file );
		return $r;
	}
	
	private function download_fsock($url, $stream = false) {
		$data = parse_url ( $url );
		
		if (! $connect = @fsockopen ( $data ['host'], 80, $errno, $errstr ))
			return false;
		
		fwrite ( $connect, "GET " . $data ['path'] . (isset ( $data ['query'] ) ? '?' . $data ['query'] : '') . " HTTP/1.1\r\n" );
		fwrite ( $connect, "Host: " . $data ['host'] . "\r\n" );
		fwrite ( $connect, "Connection: Close\r\n\r\n" );
		
		// Skip HTTP headers
		$stop = false;
		while ( ! $stop ) {
			$r = str_replace ( "\r", '', fgets ( $connect ) );
			if (strpos ( $r, '404 Not Found' ))
				return false;
			if ($r == "\n")
				$stop = true;
		}
		
		if ($stream) {
			$r = true;
			while ( ! feof ( $connect ) )
				fwrite ( $stream, fread ( $connect, 512 ) );
		} else {
			$r = '';
			while ( ! feof ( $connect ) )
				$r .= str_replace ( "\r", '', fread ( $connect, 512 ) );
		}
		fclose ( $connect );
		
		return $r;
	}
	
	private function make_dir($pathname, $mode) {
		if (! is_dir ( $pathname )) {
			$dirs = "";
			$path = explode ( '/', $pathname );
			foreach ( $path as $dir ) {
				$dirs .= $dir . "/";
				@mkdir ( $dirs, $mode );
				@chmod ( $dirs, $mode );
			}
		}
		return true;
	}

}

 
?>